package com.wswy.wzcx.ui.component

import android.graphics.Color
import android.graphics.Rect
import android.media.Image
import android.support.v4.content.ContextCompat
import android.support.v4.view.ViewCompat
import android.support.v7.content.res.AppCompatResources
import android.support.v7.widget.RecyclerView
import android.text.TextUtils
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.che.libcommon.recycler.Component
import com.wswy.wzcx.R
import com.wswy.wzcx.model.JZCFMode
import com.wswy.wzcx.ui.other.AndroidUtilities
import com.wswy.wzcx.ui.other.LayoutHelper
import com.wswy.wzcx.ui.other.ViewHelp
import com.wswy.wzcx.widget.home.RubbishFrameLayout

/**
 * Created by wangduheng26 on 2018/4/3.
 * wangduheng26@gmail.com
 */
class JZCFComponent(parent: ViewGroup) : Component<JZCFMode> {

    var textView: TextView
    var textView1: TextView
    var layout: RubbishFrameLayout
    val rootView: FrameLayout
    var delView: ImageView
    var showDelete: Boolean = false
    var listener: OnDeleteListener? = null
    var textTip: TextView

    init {
        val context = parent.context
        layout = RubbishFrameLayout(context, RubbishFrameLayout.TYPE_JZCF)
        layout.background = ContextCompat.getDrawable(context, R.drawable.shape_while)
        ViewCompat.setElevation(layout, AndroidUtilities.dpf2(2f))
//        ViewCompat.setZ(layout,AndroidUtilities.dpf2(5f))
        val relativeLayout = RelativeLayout(context)


        val imageView = ImageView(context)
        imageView.setImageResource(R.drawable.jzcf_left_icon)
        imageView.id = ViewHelp.generateViewId()
        var params = LayoutHelper.createRelative(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT)
        LayoutHelper.setMargin(params, 20, 18, 0, 0)
        relativeLayout.addView(imageView, params)


        textView = TextView(context)
        textView.id = ViewHelp.generateViewId()
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f)
        textView.setTextColor(Color.parseColor("#444444"))
        params = LayoutHelper.createRelative(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT)
        params.addRule(RelativeLayout.RIGHT_OF, imageView.id)
        LayoutHelper.setMargin(params, 10, 18, 0, 0)
        relativeLayout.addView(textView, params)


        textView1 = TextView(context)
        textView1.id = ViewHelp.generateViewId()
        textView1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f)
        textView1.setTextColor(ContextCompat.getColor(context, R.color.gray60))
        params = LayoutHelper.createRelative(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT)
        LayoutHelper.setMargin(params, 0, 12, 0, 0)
        params.addRule(RelativeLayout.ALIGN_LEFT, textView.id)
        params.addRule(RelativeLayout.BELOW, textView.id)
        relativeLayout.addView(textView1, params)

        textTip = TextView(context)
        textTip.text = "驾驶证信息有误，请重新编辑"
        textTip.setTextColor(ContextCompat.getColor(context, R.color.textSevereColor))
        textTip.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12f)

        try {
            textTip.setCompoundDrawablesWithIntrinsicBounds(AppCompatResources.getDrawable(context,R.drawable.ic_error_black_24dp),
                    null,
                    null,
                    null)
        } catch (e: Throwable) {
        }
        textTip.compoundDrawablePadding = 5
        textTip.gravity = Gravity.CENTER_VERTICAL
        params = LayoutHelper.createRelative(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT)
        params.addRule(RelativeLayout.ALIGN_LEFT, textView.id)
        params.addRule(RelativeLayout.BELOW, textView1.id)
        LayoutHelper.setMargin(params, 0, 5, 0, 5)
        relativeLayout.addView(textTip, params)
        textTip.visibility = View.GONE

        layout.addView(relativeLayout, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.MATCH_PARENT))
        LayoutHelper.setSelectable(layout)


        rootView = FrameLayout(context)
        var innerParams = LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, Gravity.CENTER)
        LayoutHelper.setMargin(innerParams, 15, 7, 15, 7)
        rootView.addView(layout, innerParams)

        delView = ImageView(context)
        delView.setImageResource(R.drawable.jzcf_del)
        innerParams = LayoutHelper.createFrame(26, 26, Gravity.RIGHT)
        LayoutHelper.setMargin(innerParams, 8,2,8,4)
        ViewCompat.setElevation(delView, AndroidUtilities.dpf2(5f))
        val layoutParams = RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
//        layoutParams.setMargins(defaultMargin, defaultMargin, defaultMargin, 0)
        rootView.layoutParams = layoutParams

        rootView.addView(delView, innerParams)
    }

    override fun getView(): View {
        return rootView
    }

    fun editMode(editMode: Boolean) {
        showDelete = editMode
    }

    override fun bind(data: JZCFMode?) {
//        super.bind(data)
//        textView.text = TextUtils.concat(data?.nikeName, "的驾驶证")
        textView.text = TextUtils.concat(data?.nikeName, "的驾驶证")
        textView1.text = data?.number
        delView.visibility = if (showDelete) View.VISIBLE else View.GONE
        delView.setOnClickListener {
            if (data != null)
                listener?.onDelete(data)
        }
        textTip.visibility = if (data?.checkBeSure()!!) View.GONE else View.VISIBLE
    }

    interface OnDeleteListener {
        fun onDelete(data: JZCFMode)
    }
}

class JZCFComponent2(parent: ViewGroup) : DataBindingComponent<Rect>(parent, R.layout.item_add_driver_license)